package com.steve.summary.two_PC;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: STEVE
 * @Description: 模拟运行
 * @since: 2025-05-21
 */
public class Main {

    public static void main(String[] args) {
        // 初始化协调者和参与者（模拟两个数据库）
        Coordinator coordinator = new Coordinator();
        Participant dbA = new Participant("DB-A");  // 初始余额100
        Participant dbB = new Participant("DB-B");  // 初始余额100
        coordinator.addParticipant(dbA);
        coordinator.addParticipant(dbB);

        // 模拟转账：A向B转账50（A扣50，B加50）
        Map<Participant, Integer> map = new HashMap<>();
        map.put(dbA, -50);  // A扣50
        map.put(dbB, 50);   // B加50
        boolean success = coordinator.execute2PC("TX-001", map);

        if (success) {
            System.out.println("Transaction succeeded!");
            System.out.println("A余额：" + dbA.getBalance());
            System.out.println("B余额：" + dbB.getBalance());
        } else {
            System.out.println("Transaction failed!");
        }
    }

}
